package com.haixiaoke.saas.repository.electronicVisa.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.saas.repository.electronicVisa.pojo.ContractRecord;
import com.haixiaoke.saas.repository.electronicVisa.service.IContractRecordService;
import com.haixiaoke.saas.repository.exception.BizException;
import com.haixiaoke.saas.repository.mapper.ContractRecordMapper;
import com.haixiaoke.saas.repository.pojo.OrgElectronicVisa;
import com.haixiaoke.saas.repository.service.IOrgElectronicVisaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 企业合同消耗记录Service业务层处理
 *
 * @author qixi
 * @date 2024-03-08
 */
@Service
public class ContractRecordServiceImpl implements IContractRecordService {
    @Autowired
    private ContractRecordMapper contractRecordMapper;
    @Autowired
    private IOrgElectronicVisaService orgElectronicVisaService;

    /**
     * 查询企业合同消耗记录
     *
     * @param recordId 企业合同消耗记录主键
     * @return 企业合同消耗记录
     */
    @Override
    public ContractRecord selectContractRecordByRecordId(Long recordId) {
        return contractRecordMapper.selectContractRecordByRecordId(recordId);
    }

    /**
     * 查询企业合同消耗记录列表
     *
     * @param contractRecord 企业合同消耗记录
     * @return 企业合同消耗记录
     */
    @Override
    public List<ContractRecord> selectContractRecordList(ContractRecord contractRecord) {
        return contractRecordMapper.selectContractRecordList(contractRecord);
    }

    /**
     * 新增企业合同消耗记录
     *
     * @param contractRecord 企业合同消耗记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertContractRecord(ContractRecord contractRecord) {

        OrgElectronicVisa orgElectronicVisa = orgElectronicVisaService.selectOrgElectronicVisaByVisaId(contractRecord.getVisaId());
        if (ObjectUtil.isNull(orgElectronicVisa)) {
            throw new BizException("企业电子签不存在");
        }
        Integer recordType = contractRecord.getRecordType();
        if (ContractRecord.RECORD_TYPE_MINUS.equals(recordType)) {
            Long contractUsedNumber = orgElectronicVisa.getContractUsedNumber();
            Long contractTotalNumber = orgElectronicVisa.getContractTotalNumber();
            Long surplusQuantity =  contractTotalNumber - contractUsedNumber;
            if (contractRecord.getNumber() > surplusQuantity) {
                throw new BizException("合同剩余数量不足无法扣减");
            }
            contractTotalNumber = contractTotalNumber - contractRecord.getNumber();
            orgElectronicVisa.setContractTotalNumber(contractTotalNumber);
            orgElectronicVisaService.updateOrgElectronicVisa(orgElectronicVisa);

        }else if (ContractRecord.RECORD_TYPE_ADD.equals(recordType)) {
            Long contractTotalNumber = orgElectronicVisa.getContractTotalNumber();
            contractTotalNumber = contractTotalNumber + contractRecord.getNumber();
            orgElectronicVisa.setContractTotalNumber(contractTotalNumber);
            orgElectronicVisaService.updateOrgElectronicVisa(orgElectronicVisa);
        }

        contractRecord.setOrgId(orgElectronicVisa.getOrgId());
        contractRecord.setOrgName(orgElectronicVisa.getOrgName());
        contractRecord.setCreateTime(new Date());
        return contractRecordMapper.insertContractRecord(contractRecord);
    }

    /**
     * 修改企业合同消耗记录
     *
     * @param contractRecord 企业合同消耗记录
     * @return 结果
     */
    @Override
    public int updateContractRecord(ContractRecord contractRecord) {
        return contractRecordMapper.updateContractRecord(contractRecord);
    }

    /**
     * 批量删除企业合同消耗记录
     *
     * @param recordIds 需要删除的企业合同消耗记录主键
     * @return 结果
     */
    @Override
    public int deleteContractRecordByRecordIds(Long[] recordIds) {
        return contractRecordMapper.deleteContractRecordByRecordIds(recordIds);
    }

    /**
     * 删除企业合同消耗记录信息
     *
     * @param recordId 企业合同消耗记录主键
     * @return 结果
     */
    @Override
    public int deleteContractRecordByRecordId(Long recordId) {
        return contractRecordMapper.deleteContractRecordByRecordId(recordId);
    }
}
